package com.tedu.API.homeWork;

import org.junit.Test;

import java.util.*;
import java.util.function.BiConsumer;

class homeWork5 {
    public static void main(String[] args) {
        //利用 HashMap 存储用户信息：id 作为键，用户作为值，创建多个用户对象进行存储
        Map<Integer, BankUser> hm = new HashMap<>();
        //存入三个值
        hm.put(1, new BankUser(1, "zhang", 22));
        hm.put(2, new BankUser(2, "wang", 23));
        hm.put(3, new BankUser(3, "liu", 888888));
        //遍历打印输出所有银行的用户名和对应的余额
        hm.forEach(new BiConsumer<Integer, Object>() {
            public void accept(Integer integer, Object o) {
                System.out.println(" " + integer + " " + o);
            }
        });
        //键盘输入一个用户名，检测是否存在此用户，存在-打印用户信息；不存在-打印查无此人
        Scanner scanner = new Scanner(System.in);
        String iName = scanner.next().trim();
        Collection<BankUser> c = hm.values();
        int coon = 0; //
        for (BankUser coll : c) {
            if (iName.equals(coll.getUserName())) {
                coon++;
                System.out.println("" + coll);
            }
            if (coon == 0) {
                System.out.println(" 查无此人");
            }
        }
        //打印输出余额大于 200000 的用户信息
        for (BankUser b : c) {
            if (b.getBalance() > 20000) {
                System.out.println(" " + b);
            }
        }

        //从命令行读入一个字符串，表示一个年份，输出该年的世界杯冠军是哪支球队。如果该年没有举办世界
        //杯，则输出：没有举办世界杯。
        System.out.println("请输入年分");
        String yearS = scanner.next().trim();
        if (numberN(yearS)) {
        } else {
            throw new RuntimeException("请输入正确年份");
        }
        Integer year = Integer.parseInt(yearS);
        //创建集合
        Map<Integer, String> worldCup = new HashMap<>();
        //region my custom region  worldCup.put
        worldCup.put(1930, "乌拉圭");
        worldCup.put(1934, "意大利");
        worldCup.put(1938, "意大利");
        worldCup.put(1950, "乌拉圭");
        worldCup.put(1954, "西德");
        worldCup.put(1958, "巴西");
        worldCup.put(1962, "巴西");
        worldCup.put(1966, "英格兰");
        worldCup.put(1970, "巴西");
        worldCup.put(1974, "西德");
        worldCup.put(1978, "阿根廷");
        worldCup.put(1982, "意大利");
        worldCup.put(1986, "阿根廷");
        worldCup.put(1990, "西德");
        worldCup.put(1994, "巴西");
        worldCup.put(1998, "法国");
        worldCup.put(2002, "巴西");
        worldCup.put(2006, "意大利");
        //endregion
        //查询
        Set<Integer> years = worldCup.keySet();
        Iterator<Integer> it = years.iterator();
        while (it.hasNext()) {
            int i = it.next();
            if (i == year) {
                System.out.println(" " + worldCup.get(i));
            }
        }
        //问题：2开头会发生throw，但是1开头不会，怀疑正则写错
        //拿到值，进行判断
        System.out.println("请输入球队");
        //问题：会自动跳过scanner.nextLine()
        scanner.nextLine();
        String cupName=scanner.nextLine().trim();
        if (worldCup.containsValue(cupName)){
            Set<Integer> keys=worldCup.keySet();
            for (Integer key:keys){
                if (cupName.equals(worldCup.get(key))){
                    System.out.println(""+cupName+" "+worldCup.keySet());
                }
            }
        }else{
            System.out.println("未获得");
        }
    }
    //正则判断
    public static boolean numberN(String str) {
        boolean flag = false;
        String regex = "^[1-9]*$";
        if (str.matches(regex)) return flag = true;
        return flag;
    }
}

//定义一个银行用户 BankUser 类，用户信息为用户 id、用户名 userName、余额 balance;
class BankUser {
    private int id;
    private String userName;
    private long balance;
    //set get

    public long getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public long getBalance() {
        return balance;
    }

    public void setBalance(long balance) {
        this.balance = balance;
    }

    //构造器

    public BankUser() {
    }

    public BankUser(int id, String userName, long balance) {
        this.id = id;
        this.userName = userName;
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "BankUser{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", balance=" + balance +
                '}';
    }
}